package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 17/08/24.
 */
@Controller
@RequestMapping(ControllerConstants.ADRESS)
public class AddressMCtronller extends BaseMobileController {
    @Autowired
    private AddressService addressService;

    /**
     * 查询用户收货地址
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getAddressList")
    public void getAddressList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int userId = getInt(s, "userId", 0);
                return addressService.getAddressList(userId);
            }
        });
    }

    /**
     * 添加用户收货地址
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addAddress")
    public void addAddress(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int userId = getInt(s, "userId", 0);
                String address = getString(s, "address", "");
                String area = getString(s, "area", "");
                String telephone = getString(s, "telephone", "");
                String name = getString(s, "name", "");
                String houseNumber = getString(s, "houseNumber", "");
                double ulng = getDouble(s, "uLng", 0);
                double ulat = getDouble(s, "uLat", 0);
                int isDefault = getInt(s, "isDefault", 1);
                int cityId = getInt(s, "cityId", 0);
                int areaId = getInt(s, "areaId", 0);
                return addressService.addAddress(userId, address, area, telephone, name, isDefault, houseNumber, ulng, ulat, cityId, areaId);
            }
        });
    }

    /**
     * 修改用户收货地址
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateAddress")
    public void updateAddress(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int userId = getInt(s, "userId", 0);
                int id = getInt(s, "id", 0);
                String address = getString(s, "address", "");
                String area = getString(s, "area", "");
                String telephone = getString(s, "telephone", "");
                String houseNumber = getString(s, "houseNumber", "");
                double ulng = getDouble(s, "uLng", 0);
                double ulat = getDouble(s, "uLat", 0);
                String name = getString(s, "name", "");
                int isDefault = getInt(s, "isDefault", 1);
                int cityId = getInt(s, "cityId", 0);
                int areaId = getInt(s, "areaId", 0);
                return addressService.updateAddress(id, userId, address, area, telephone, name, isDefault, houseNumber, ulng, ulat, cityId, areaId);
            }
        });
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteDeliveryAddressById")
    public void deleteDeliveryAddressById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {

            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                Long id = getLong(s, "id", 0L);
                return addressService.deleteDeliveryAddressById(id);
            }
        });

    }

    /**
     * 修改为默认地址
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateDefaultAddress")
    public void updateDefaultAddress(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {

            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                Integer id = getInteger(s, "id", 0);
                Integer userId = getInt(s, "userId", 0);
                return addressService.updateDefaultAddress(id, userId);
            }
        });

    }

    /**
     * 获取默认地址
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getDefaultAddress")
    public void getDefaultAddress(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {

            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                Integer userId = getInt(s, "userId", 0);
                return addressService.getDefaultAddress(userId);
            }
        });

    }

    /**
     * 根据城市名称获取城市编号
     */
    @RequestMapping("/getCityIdByName")
    public void getCityIdByName(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {

            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                String cityName = getString(s, "cityName", "");
                Integer level = getInt(s, "level", 3);//国家 ： 1  ， 省 ： 2   ，市：  3 ，区 4
                return addressService.getCityIdByName(cityName, level);
            }
        });

    }
}
